/*
 * 打印功能（可以打印所有的医嘱单，多选，西药打印多个打印类型）
 */

function printMultipleAdvice()
{
	$(".print_btn").attr("disabled", true);
	layer.closeAll();
	var ispass = true;
	var choseArr = $(".innerHead td input:checked").parents("tbody").find(".yizhu_details"); //拿到所勾选的医嘱单
	if (choseArr.length < 1)
	{
		layer.msg("请勾选要打印的医嘱单",
		{
		time : 2000,
		icon : 0,
		skin : 'layui-layer-msg0'
		});
		$(".print_btn").removeAttr("disabled");
		return false;
	}

	$(choseArr).each(function(i, ele)
	{
		var dataStr = $(ele).attr("data-info");
		var dataObj = JSON.parse(dataStr);
		var vaf10 = dataObj.vaf10;
		var cur = $(ele).parents("tbody").find(".innerHead td:eq(0)");
		if (vaf10 != 8)
		{
			layer.tips('请先发送医嘱单', cur,
			{
				tipsMore : true
			});
			$(".print_btn").removeAttr("disabled");
			ispass = false;
			return false;
		}
		else if (vaf10 == '4')
		{
			layer.tips('医嘱已作废，请重开!', cur,
			{
				tipsMore : true
			});
			$(".print_btn").removeAttr("disabled");
			ispass = false;
			return false;
		}
	})
	if (ispass)
	{
		debugger;
		$(choseArr).each(function(i, ele)
		{
			var dataStr = $(ele).attr("data-info");
			var dataObj = JSON.parse(dataStr);
//			cbm01 = dataObj.cbm01;
			addPrintFormat(dataObj);
		});
		finallyPrint();
	}
}
//----------------------------------------------------开始打印-----------------------------------------------------------------------------//

var printAdviceArr=[]; //打印所有处方
var printShuyeArr = [];
//加载打印格式

function justXiyaoType(cbm07,rid){
	//普通、急诊
	if(cbm07 == "1"||cbm07 == "2"||(cbm07 == "6")){
		rid = 24;
	}
	else if(cbm07 == "3"){ //儿童
		rid = 103;
	}
	//毒麻
	else if((cbm07 == "4")||(cbm07 == "5")||(cbm07 == "8")){
		rid = 89;
	}
	return rid;
}
function addPrintFormat(dataObj)
{
	var bda01 = dataObj.bda01;
	var vaf10 = dataObj.vaf10;
//	var cbm01 = dataObj.cbm01;
	var rid;
	debugger;
	switch (bda01)
	{
		case "1" :
			rid = justXiyaoType(dataObj.cbm07,rid);
			break;
		case "2" :
			rid = justXiyaoType(dataObj.cbm07,rid);
			break;
		case "8" :
			rid = 25;
			break;
		case "E" :
			rid = 22;
			break;
		case "L" :
			rid = 23;
			break;
		case "S" :
			rid = 28;
			break;
		case "4" :
			rid = 190;//待定
			break;
		case "M" :
			rid = 190;//待定
			break;
		default ://治疗
			rid = 190;
			break;
	}
	
		var url = printUrl + "/printtemprest/findListByRid/" + rid + "?jsessionids=" + $.cookie('jsessionids');
		var para =
		{};
		$.ajax(
		{
		url : url,
		type : "get",
		async : false,
		dataType : "json",
		traditional : true,
		success : function(data)
		{
			var defaultV2;
			if (data)
			{
				var obj = {};
				obj.vaf10 = vaf10;
				obj.cbm01 = dataObj.cbm01;
				var html = "";
				for (var i = 0; i < data.length; i++)
				{
					html += '<option data-info=' + JSON.stringify(obj)+ ' value=' + data[i].id +' >' + data[i]['name'] + '</option>';
					if (data[i]['xdefault'] == "1")
					{
						defaultV2 = data[i].id;
					}
				}
				$("#advice_print").empty().append(html);
				$("#advice_print").select2();
				if (defaultV2)
				{
					$("#advice_print").val(defaultV2).trigger("change");
				}
				obj.id = $("#advice_print").val();
				
				printAdviceArr.push(obj);
					
				if (bda01 == "1" || bda01 == "2"){
					debugger;
					obj.vaf01a = dataObj.vaf01a;
					printXiYao(obj);
				}
			}
		}
		})
//	}
//如果是西药，则加载输液卡和皮试单
}
//打印所有单子
function  finallyPrint(){
	var cbm01Arr=[];
	var cbm01Arr1=[];
	for (var k = 0; k < printAdviceArr.length; k++)
	{
		if($.inArray(printAdviceArr[k].cbm01,cbm01Arr)<0)
		{
//			send_print(duizhaoObj[printArr[k]['bda01']], printArr[k]['cbm01']);
			printImmediate(printAdviceArr[k]);
			cbm01Arr.push(printAdviceArr[k].cbm01);
		}
	}
	if (valueArr['p_9999_9999_98']=="1")
	{
		//输液单
		for (var k = 0; k < printShuyeArr.length; k++)
		{
			if($.inArray(printShuyeArr[k].cbm01,cbm01Arr1)<0)
			{
	//			send_print(duizhaoObj[printArr[k]['bda01']], printArr[k]['cbm01']);
				printImmediate(printShuyeArr[k]);
				cbm01Arr1.push(printShuyeArr[k].cbm01);
			}
		
		}
		
		//皮试单 --- 深圳宝田医院暂定不打印
		/*for (var k = 0; k < shuyeArr.length; k++)
		{
			send_print(12, printArr[k]['cbm01']);
		}*/
	}
	printAdviceArr.length = 0;
	printShuyeArr.length = 0;
}



//西药打印单特殊处理
function printXiYao(objp){
	//输液卡URL
	var html1;
	$.ajax(
			{
			url : printUrl + "/printtemprest/findListByRid/" + 101 + "?jsessionids=" + $.cookie('jsessionids'),
			type : "get",
			async : false,
			dataType : "json",
			traditional : true,
			success : function(data)
			{
				var defaultV2;
				var obj = {};
				obj.vaf10 = objp.vaf10;
				obj.vaf01a = objp.vaf01a;
				obj.cbm01 = objp.cbm01;
				for (var i = 0; i < data.length; i++)
				{
					
					if (data[i]['xdefault'] == "1")
					{
						defaultV2 = data[i].id;
						html1 += '<option data-info=' + JSON.stringify(obj) + ' value=' + data[i].id + '>' + data[i]['name'] + '</option>';
					}
					
				}
				$("#advice_print_shuye").empty().append(html1);
				$("#advice_print_shuye").select2();
				if (defaultV2)
				{
					$("#advice_print_shuye").val(defaultV2).trigger("change");
				}
				obj.id = $("#advice_print_shuye").val();
				Bbx20Search(obj);
			}
		});
	//皮试单
//	var html2 = "";
//	$.ajax(
//			{
//			url : printUrl + "/printtemprest/findListByRid/" + 102 + "?jsessionids=" + $.cookie('jsessionids'),
//			type : "get",
//			async : false,
//			dataType : "json",
//			traditional : true,
//			success : function(data)
//			{
//				var defaultV3;
//				for (var i = 0; i < data.length; i++)
//				{
//					var obj =
//					{};
//					obj.vaf10 = vaf10;
//					obj.id = data[i]['id'];
//					if (data[i]['xdefault'] == "1")
//					{
//						defaultV3 = data[i].id;
//						html2 += '<option data-info=' + JSON.stringify(obj) + '  value=' + data[i].id + '>' + data[i]['name'] + '</option>';
//					}
//					else
//					{
//						html2 += '<option data-info=' + JSON.stringify(obj) + '  value=' + data[i].id + '>' + data[i]['name'] + '</option>';
//					}
//				}
//				$("#advice_print_pishi").empty().append(html2);
//				$("#advice_print_pishi").select2();
//				if (defaultV3)
//				{
//
//					$("#advice_print_pishi").val(defaultV3).trigger("change");
//				}
//				if(obj.vaf23=="皮试( )"){
//					 printImmediate(obj);
//				}
//			}
//			});

}

//判断是否打印输液卡
function Bbx20Search(objp){
	var url = rootUrl+"/clidoctor/querybbx20Byvaf01";
	$.ajax(
			{
			url : url,
			type : "get",
			async : false,
			dataType : "json",
			data:{vaf01:objp.vaf01a},
			success : function(data)
			{
				if(data.list[0].bbx20=="2" || data.list[0].bbx20=="1"){
					printShuyeArr.push(objp);
//					printImmediate(objp);
				}
			}
			})
}



function printImmediate(obj)
{

	if (obj.vaf10 == '8')
	{
		var url = printUrl + "/printtoolrest/printToPdf/" + obj.id + "?cbm01=" + obj.cbm01;
		layer.open(
		{
		type : 2,
		title : false, // title 不显示
		closeBtn : 0, // 不显示  layer 弹框关闭按钮
		shadeClose : false,
		shade : false,
		area :
		['0%', '0%'], //在页面上不显示
		content : url, //iframe的url,
		success : function()
		{
			$(".print_btn").removeAttr("disabled");
		}
		});
	}
	
}
/*-----------------------------------------------------医嘱单合并打印---------------------------------------------------------------------*/
//打印选中医嘱单，添加打印类型
function printConbinChecked(flag){
	if ($(".innerHead td input:checked").length <= 0)
	{
		layer.msg("请勾选要打印的医嘱单",
		{
		time : 2000,
		icon : 0,
		skin: 'layui-layer-msg0'
		});
		$(".print_btn").removeAttr("disabled");
		return;
	}
	else
	{
		var cbm01s = "{";
		var cbm01A = [];
		$(".innerHead td input:checked").each(function(i,ele){
			var dataStr = $($(ele).parents("tbody").find(".yizhu_details")[0]).attr("data-info");
		    var dataObj = JSON.parse(dataStr);
		    var vaf10 = dataObj.vaf10;
		    if(vaf10!=8)
			{
				layer.msg("请先发送医嘱单",
						{
						time : 2000,
						icon : 0,
	    				skin: 'layui-layer-msg0'
						});
				$(".print_btn").removeAttr("disabled");
						return;
			}
		    cbm01 = dataObj.cbm01;
		    cbm01A.push(cbm01);
		});
			var cbm01S = cbm01A.join(",");
			cbm01s +=cbm01S + "}"
			
			printConbinAdvice(cbm01s,flag);//vaf10 判断是否是医嘱单状态
		}
}
//加载打印格式下拉列表
function printConbinAdvice(cbm01s,flag) {
	debugger;
	var rid = 104;
	var html ="";
	var myAjax = $.ajax({
		url : printUrl + "/printtemprest/findListByRid/" + rid+"?jsessionids="+$.cookie('jsessionids'),
		type : "get",
		async : true,
		dataType : "json",
		traditional : true,
		success : function(data) {
			var defaultV1;
			for (var i = 0; i < data.length; i++) {
				
				if (data[i]['xdefault'] == "1") {
					defaultV1 = data[i].id;
					 html += '<option value='+data[i].id+'>'+data[i]['name']+'</option>';
				} else {
					 html += '<option value='+data[i].id+'>'+data[i]['name']+'</option>';
				}
			}
			$("#advice_print_c").empty().append(html);
			$("#advice_print_c").select2();
			if(defaultV1){
				
				$("#advice_print_c").val(defaultV1).trigger("change");
			}
			
			else
			{
				$("#advice_print_shuye_c").empty();
				$("#advice_print_pishi_c").empty();
				$(".iswest").hide();
			}
			if(flag){
				var id = $("#advice_print_c option").val();
				if(id){
					var url = printUrl + "/printtoolrest/printToPdf/" + id + "?cbm01s="+ encodeURIComponent(cbm01s)+"&jsessionids="+$.cookie('jsessionids');
					layer.open(
							{
							type : 2,
							title : false, // title 不显示
							closeBtn : 0, // 不显示  layer 弹框关闭按钮
							shadeClose : false,
							shade : false,
							area :
							['0%', '0%'], //在页面上不显示
							content : url, //iframe的url,
							success : function()
							{
								$(".print_btn").removeAttr("disabled");
							}
							});
				}
				
			}else{
				
				printCombinView(cbm01s); //打印预览
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			layer.msg('打印服务器连接失败！', {
				time : 2000,
				icon : 3,
				skin: 'layui-layer-msg3'
			});
			$(".print_btn").removeAttr("disabled");
		}
	});

}
//打印预览
function printCombinView(cbm01s){
	$("#combin_print_ul .print_to_view").off("click").on("click",function(){
		var id = $("#advice_print_c option").val();
		if(id){
			var url = printUrl + "/printtoolrest/printToPdfView/" + id + "?cbm01s="+ encodeURIComponent(cbm01s)+"&jsessionids="+$.cookie('jsessionids');
			window.open(url);
		}else{
			layer.msg("请选择打印的格式！",{time:2000,icon:0,skin: 'layui-layer-msg0'});
		}
		
	})
}
//直接打印
function printCombin(){
	$(".print_btn").attr("disabled",true);

	printConbinChecked(true); //直接打印
}

/*-----------------------------------------------------医嘱单合并打印---------------------------------------------------------------------*/
//	var myAjax = $.ajax({
//		url : printUrl + "/printtemprest/findListByRid/" + rid+"?jsessionids="+$.cookie('jsessionids'),
//		type : "get",
//		async : true,
//		dataType : "json",
//		traditional : true,
//		success : function(data) {
//			$(".print_btn").removeAttr("disabled");
//			var html ="";
//			for (var i = 0; i < data.length; i++) {
//				var obj = {};
//				obj.vaf10 = vaf10;
//				obj.id = data[i]['id'];
//				if (data[i]['xdefault'] == "1") {
//					html += '<option data-info='+JSON.stringify(obj)+'>'+data[i]['name']+'</option>';
//				} else {
//					html += '<option data-info='+JSON.stringify(obj)+'>'+data[i]['name']+'</option>';
//				}
//			}
//			$("#advice_print").empty().append(html);
//		}
//	})

